package com.ea.controller.back;

import com.ea.api.controller.admin.AdminInfoApi;
import com.ea.common.enums.OperationResultCode;
import com.ea.common.exception.ServiceException;
import com.ea.common.response.JsonResult;
import com.ea.common.util.TypeConversionToJson;
import com.ea.domain.dto.AdminDTO;
import com.ea.domain.dto.LoginCreateDTO;
import com.ea.domain.dto.LoginDTO;
import com.ea.domain.dto.LoginUpdateDTO;
import com.ea.domain.dto.PasswordChangeDTO;
import com.ea.domain.entity.AdminInfo;
import com.ea.domain.query.AdminQueryDTO;
import com.ea.service.IAdminInfoService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 管理员信息表 前端控制器
 * </p>
 *
 * @author 电器商城
 * @since 2022-11-29
 */
@RestController
@RequestMapping("/electric/back/admin")
public class AdminInfoController implements AdminInfoApi {

    private static final Logger logger = LoggerFactory.getLogger(AdminInfoController.class);

    @Autowired
    private IAdminInfoService adminInfoService;

    @Override
    @PostMapping("/loginIn")
    public JsonResult adminLoginIn(@RequestBody LoginDTO loginDTO) {
        AdminDTO adminDTO = adminInfoService.adminLoginIn(loginDTO);
        return JsonResult.loginSuccessFul(adminDTO);
    }

    @Override
    @GetMapping("/loginOut")
    public JsonResult adminLoginOut() {
        adminInfoService.adminLoginOut();
        return JsonResult.loginOut();
    }

    @Override
    @PostMapping("/get/list")
    public JsonResult getAdminInfoList(@RequestBody AdminQueryDTO queryDTO) {
        return JsonResult.doSuccessful(adminInfoService.getAdminInfoList(queryDTO));
    }

    @Override
    @PostMapping("/create")
    public JsonResult createAdminInfo(@RequestBody LoginCreateDTO createDTO) {
        return JsonResult.doSuccessful(adminInfoService.createAdminInfo(createDTO));
    }

    @Override
    @PostMapping("/change")
    public JsonResult updateAdminInfo(@RequestBody LoginUpdateDTO updateDTO) {
        adminInfoService.updateAdmin(updateDTO);
        return JsonResult.doSuccessful();
    }

    @Override
    @GetMapping ("/wirte/off")
    public JsonResult writeOffAdmin(@RequestParam("adminId") Long adminId) {
        if (adminId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        adminInfoService.writeOffAdmin(adminId);
        return JsonResult.delSuccessFul();
    }

    @Override
    @PostMapping("/change/password")
    public JsonResult changePassword(@RequestBody PasswordChangeDTO changeDTO) {

        if (changeDTO == null || StringUtils.isEmpty(changeDTO.getOldPassword()) || StringUtils.isEmpty(changeDTO.getNewPassword())) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        adminInfoService.changePassword(changeDTO.getOldPassword(), changeDTO.getNewPassword());
        return JsonResult.updateSuccessFul();
    }

    @Override
    @GetMapping("/reset")
    public JsonResult resetPassword(@RequestParam("adminId") Long adminId) {
        if (adminId == null ) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        return JsonResult.doSuccessful(adminInfoService.resetPassword(adminId));
    }
}
